import { Context, controller, inject, provide, post,plugin} from 'midway';
import { IAccountService} from '../interface';

@provide()
@controller('/user')
export class AccountController {

  @inject()
  ctx: Context;
  @plugin()
  jwt
  @inject('accountService')
  service: IAccountService;

  @post('/login')
  async login(): Promise<void> {
      const {userName,password,type,captcha,mobile} = this.ctx.request.body;
      if(type==='account'){
         const account ={userName,password}
         const res:any =await this.service.getAccount(account)
         const token = this.jwt.sign({
            res
          });
         if(res.length>0&&res!=undefined){
            this.ctx.body = {success: true, message: 'OK',status:res,token};
         }else{
            this.ctx.body = {success: false, message: 'error'};
         }
      }else {
         const session = this.ctx.session.verification
         for(let item of session){
            if(item.phone==mobile&&item.code==captcha){
               console.log(mobile)
               this.ctx.body = {success: true, message: '验证码或手机号输入成功！'};
            }else {
               this.ctx.body = {success: false, message: '验证码或手机号输入错误！'};
            }
            
         }
      }

  }
}
